package view;

import controller.*;
import pojo.User;
import service.UserService;

import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.List;

public class UserView extends JPanel {
    private UserViewListener userViewListener;
    private List<User> userList;
    private User user;
    private String userId;
    private JScrollPane scroll;
    private MainListener listener;
    private UserDetailsListener userDetailsListener;
    private JButton jButton1,jButton2;
    private UserClickListener userClickListener;
    private JLabel labelSelf;
    public UserView(List<User> userList,User user){
        userViewListener=new UserViewListener(this);
        userClickListener=new UserClickListener(this);
        this.userList=userList;
        this.user=user;
        this.setLayout(new BorderLayout());
        this.add(this.getLabel(),BorderLayout.NORTH);
        this.add(this.getPanel(),BorderLayout.CENTER);
        this.add(this.getButton(),BorderLayout.SOUTH);
    }

    //刷新
    public void refresh(){
        UserService userService=new UserService();
        userList = userService.searchAll();
        this.remove(scroll);
        this.add(this.getPanel());
        //刷新
        this.validate();
    }


    public JPanel getLabel(){
        JPanel panel=new JPanel();
        labelSelf=new JLabel("所有用户信息");
        labelSelf.setFont(( new Font( "隶书" , Font.BOLD , 30)));
        panel.add(labelSelf);
        return panel;
    }
    public JScrollPane getPanel(){
        String[] titles={"用户id","用户名","用户类别"};
        Object data[][] = new Object[userList.size()][3];
        Iterator<User> iter = userList.iterator();
        int i = 0;
        while (iter.hasNext()) {
            User user= iter.next();
            data[i][0] = user.getUserId();
            data[i][1] = user.getUserName();
            data[i][2]=user.getType();
            i++;
        }
        JTable table = new JTable(data,titles);
        table.addMouseListener(userClickListener);
        userClickListener.setTable(table);
        scroll = new JScrollPane(table);
        return scroll;
    }

    public JPanel getButton(){
        JPanel jPanel=new JPanel();
        jButton1=new JButton("新增用户");
        jButton1.addActionListener(userViewListener);
        jButton2=new JButton("");
        jPanel.add(jButton1);
        return jPanel;
    }
}
